package com.example.demo.calc;

import com.example.demo.po.ObjectPo;
import org.apache.lucene.util.RamUsageEstimator;

import java.util.HashMap;
import java.util.Map;

public class StringBuilderDemo {

    public static void main(String[] args) {

        System.out.println(RamUsageEstimator.sizeOfObject(new Object()));

        Map<String, Object> map = new HashMap();
        map.put("zhangsan", new ObjectPo());
        System.out.println("map的大小" + RamUsageEstimator.sizeOfMap(map));

        String str = "1234566789";
        System.out.println("字符串的大小" + RamUsageEstimator.sizeOf(str));

        System.out.println("对象的大小" + RamUsageEstimator.sizeOfObject(new ObjectPo()));

        byte[] b4 = new byte[7 * 1024 * 1024];
        System.out.println("字节数组的大小" + RamUsageEstimator.sizeOf(b4));

        int tempSize = 0;
        for (int i = 0; i < 1000; i++) {
            StringBuilder builder = new StringBuilder();
            builder.append(str);
            builder.append(str);
            builder.append(str);
            builder.append(str);
            tempSize += RamUsageEstimator.sizeOfObject(builder);
        }
        System.out.println("StringBuilder大小：" + tempSize);

        StringBuilder builder2 = new StringBuilder();
        for (int i = 0; i < 1000; i++) {
            builder2.append(str);
            builder2.append(str);
            builder2.append(str);
            builder2.append(str);
        }
        System.out.println("StringBuilder2大小：" + RamUsageEstimator.sizeOfObject(builder2));
    }

}
